package com.example.mindadinner.manger;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.example.mindadinner.adapter.PagerAapterForViews;

import android.content.Context;
import android.view.View;

public class ViewPagerManger {
	private static ViewPagerManger instance;
	private Context con;

	private ViewPagerManger(Context con) {
		this.con = con;
	}

	public static ViewPagerManger getInstance(Context con) {
		if (instance == null) {
			instance = new ViewPagerManger(con);
		}
		return instance;
	}

	public PagerAapterForViews getMiddleViewPagerAdapter() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException  {
		List<View> viewList = new ArrayList<View>();
		List<String> titleList = new ArrayList<String>();
		
		Properties pro = new Properties();
		InputStream ins = ViewPagerManger.class.getResourceAsStream("/assets/ui.properties");
		pro.load(ins);
		String []titles=pro.getProperty("titles").split(",");
		for(String title:titles){
			titleList.add(title);
		}
		String[] sClazzs = pro.getProperty("middleViewFragments").split(",");
		String packageName=con.getPackageName();
		for (String sClazz : sClazzs) {
			sClazz=packageName+"."+sClazz;
			Class<?> clazz = Class.forName(sClazz);
			Constructor<?> constructor =clazz.getConstructor(Context.class);
			Object obj=constructor.newInstance(con);
			View v = (View) clazz.getMethod("getMiddleViewViewPagerItemView",null).invoke(obj, null);
			viewList.add(v);
		}
		ins.close();
		return new PagerAapterForViews(viewList, titleList);
	}
}
